package com.ruoyi.project.system.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.service.SysLoginService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/system/mobile")
public class MobileController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private IMovieService movieService;

    @Autowired
    private ISubscribeService subscribeService;

    @Autowired
    private ICinemaService cinemaService;

    @Autowired
    private IMovieShowService movieShowService;

    @Autowired
    private ISeatService seatService;

    @Autowired
    private IPayService payService;

    @Autowired
    private IMovieShowRelationService movieShowRelationService;
    


    @PostMapping("/register")
    public AjaxResult add( @RequestBody SysUser user)
    {
        if (UserConstants.NOT_UNIQUE.equals(sysUserService.checkUserNameUnique(user.getUserName())))
        {
            return AjaxResult.error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
//        SysUser sysUser = sysUserService.selectUserByUserName(user.getUserName());
//        List<SysUser> sysUsers = sysUserService.selectUserList(new SysUser());
//        System.out.println(sysUsers);
//        user.setCreateBy(SecurityUtils.getUsername());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return toAjax(sysUserService.insertUser(user));
    }

    @PostMapping("/login")
    public AjaxResult login( @RequestBody SysUser user)
    {
        AjaxResult ajax = AjaxResult.success();
        List<SysUser> sysUsers = sysUserService.selectUserList(user);
        if(sysUsers.size()==0){
            return AjaxResult.error("用户名密码填写错误");
        }
        // 生成令牌
        String token = loginService.login(user.getUserName(), user.getPassword());
        ajax.put(Constants.TOKEN, token);
        ajax.put("user",sysUsers.get(0));
        return ajax;
    }

    @PostMapping("/movieHot")
    public AjaxResult movieHot()
    {
        List<Movie> movies = movieService.selectMovieList(new Movie());
        return AjaxResult.success(movies);
    }

    @PostMapping("/subscribe")
    public AjaxResult subscribe(@RequestBody Movie movie)
    {
        Subscribe sub = new Subscribe();
        sub.setUserId(movie.getUserId());
        sub.setMovieId(movie.getId());
        List<Subscribe> subscribes = subscribeService.selectSubscribeList(sub);
        if(subscribes.size()>0){
            return AjaxResult.error("当前电影已经订阅过，不可重复订阅");
        }
        Subscribe subscribe = new Subscribe();
        subscribe.setId(UUID.randomUUID().toString().replace("-", ""));
        subscribe.setMovieId(movie.getId());
//        SysUser user = SecurityUtils.getLoginUser().getUser();
        subscribe.setUserId(movie.getUserId());
        subscribeService.insertSubscribe(subscribe);
        return AjaxResult.success();
    }

    @PostMapping("/mySubscribe")
    public AjaxResult mySubscribe(@RequestBody Movie movie)
    {
        Subscribe subscribe = new Subscribe();
        subscribe.setUserId(movie.getUserId());
        List<Subscribe> subscribeList = subscribeService.selectSubscribeList(subscribe);
        List<Movie> movieList = new ArrayList<>();
        for(Subscribe cubscribe : subscribeList){
            Movie movie1 = movieService.selectMovieById(cubscribe.getMovieId());
            movieList.add(movie1);
        }
        return AjaxResult.success(movieList);
    }

    @PostMapping("/cinemaList")
    public AjaxResult cinemaList()
    {
        List<Cinema> cinemaList = cinemaService.selectCinemaList(new Cinema());
        return AjaxResult.success(cinemaList);
    }

    @PostMapping("/movieList")
    public AjaxResult movieList(@RequestBody MovieShow movieShow)
    {
        MovieShowRelation movieShowRelation = new MovieShowRelation();
        movieShowRelation.setShowId(movieShow.getId());
        List<Movie> finalList = new ArrayList<>();
        List<MovieShowRelation> movieShowRelations = movieShowRelationService.selectMovieShowRelationList(movieShowRelation);
        for(MovieShowRelation showRelation : movieShowRelations){
            Movie movie = new Movie();
            movie.setId(showRelation.getMovieId());
            List<Movie> movieList = movieService.selectMovieList(movie);
            finalList.addAll(movieList);
        }

        Pay pay = new Pay();
        pay.setShowId(movieShow.getId());
        List<Pay> payList = payService.selectPayList(pay);
        for(Movie movie : finalList){
            for(Pay pau :payList ){
                if(movie.getId().equals(pau.getMovieId())){
                    movie.setMovieNum(movie.getMovieNum()+1);
                    movie.setSeatNum(movie.getSeatNum()+1);
                }
            }
        }
        for(Movie movie : finalList){
            movie.setMovieNum(40-movie.getMovieNum());
            movie.setSeatNum(40-movie.getSeatNum());
        }
        return AjaxResult.success(finalList);
    }

    @PostMapping("/selectShow")
    public AjaxResult selectShow(@RequestBody Cinema cinema)
    {
        MovieShow movieShow = new MovieShow();
        movieShow.setCinemaId(cinema.getId());
        List<MovieShow> movieShows = movieShowService.selectMovieShowList(movieShow);
        return AjaxResult.success(movieShows);
    }

    @PostMapping("/selectSeat")
    public AjaxResult selectSeat(@RequestBody MovieShow movieShow)
    {
        Seat seat = new Seat();
        seat.setShowId(movieShow.getId());
        List<Seat> seatList = seatService.selectSeatList(seat);
        return AjaxResult.success(seatList);
    }

    @PostMapping("/pay")
    public AjaxResult pay(@RequestBody Pay pay)
    {
        SysUser sysUser = sysUserService.selectUserById(Long.valueOf(pay.getUserId()));
        pay.setUserName(sysUser.getNickName());
        Movie movie = movieService.selectMovieById(pay.getMovieId());
        pay.setMovieName(movie.getName());
        Cinema cinema = cinemaService.selectCinemaById(pay.getCinemaId());
        pay.setCinemaName(cinema.getName());
//        Seat seat = seatService.selectSeatById(pay.getSeatId());
//        pay.setSeatName(seat.getName());
        pay.setTime(new Date());
        pay.setId(UUID.randomUUID().toString().replace("-", ""));
        payService.insertPay(pay);
        if(movie.getSeeNum()==null){
            movie.setSeeNum(1l);
        }else{
            movie.setSeeNum(movie.getSeeNum()+1);
        }
        movieService.updateMovie(movie);
        return AjaxResult.success();
    }

    @PostMapping("/myPay")
    public AjaxResult myPay(@RequestBody Pay pay)
    {
        List<Pay> payList = payService.selectPayList(pay);
        return AjaxResult.success(payList);
    }
}
